package cn.xn.hrms.api.controller;

import cn.xn.hrms.domain.service.PersonalService;
import cn.xn.hrms.infrastructure.dataobject.PersonalSelectDO;
import cn.xn.hrms.infrastructure.dataobject.PersonalDO;
import cn.xn.hrms.domain.utils.ResultVo;
import cn.xn.hrms.api.dto.req.PersonalReqDTO;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Set;

/**
 * @author ysz
 */
@RestController
public class PersonalController {

    @Autowired
    PersonalService personalService;

    /**
     * 分页查询员工列表
     *
     * @param page
     * @param size
     * @param departmentId
     * @param personalName
     * @param workStatus
     * @return
     */
    @GetMapping("/personal/list")
    @ApiOperation("分页查询员工列表")
    public PageInfo<PersonalDO> list(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                     @RequestParam(value = "size", defaultValue = "10") Integer size,
                                     @RequestParam(value = "departmentId", defaultValue = "0") Integer departmentId,
                                     @RequestParam(value = "personalName", required = false) String personalName,
                                     @RequestParam(value = "workStatus", defaultValue = "0") Integer workStatus) {

        return personalService.findAll(page, size, departmentId, personalName, workStatus);
    }

    /**
     * 员工列表
     *
     * @return
     */
    @GetMapping("/personal/all")
    @ApiOperation("员工列表")
    public List<PersonalDO> all() {
        return personalService.All();
    }

    /**
     * 新增员工
     *
     * @param personalReqDTO
     * @return
     */
    @PostMapping("/personal")
    @ApiOperation("新增员工")
    public ResultVo insert(@RequestBody @Valid PersonalReqDTO personalReqDTO) {
        personalService.insert(personalReqDTO);
        return ResultVo.success();
    }

    /**
     * 修改员工
     *
     * @param personalReqDTO
     * @param id
     * @return
     */
    @PutMapping("/personal/{id}")
    @ApiOperation("修改员工")
    public ResultVo update(@RequestBody PersonalReqDTO personalReqDTO, @PathVariable Integer id) {
        personalService.updateById(id, personalReqDTO);
        return ResultVo.success();
    }

    /**
     * 删除员工（支持批量）
     * @param id
     * @return
     */
    @DeleteMapping("/personal/delete")
    @ApiOperation("删除员工（支持批量）")
    public ResultVo deleteIdIn(@RequestBody Set<Integer> id) {
        personalService.deleteByIdIn(id);
        return ResultVo.success();
    }
}
